  /**
  ******************************************************************************
  * @file    
  * @author  Tmax Sco
  * @version 
  * @date   
  * @brief   This file contains the headers of 
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __ctrl_h
#define __ctrl_h

/* Includes ------------------------------------------------------------------*/
#include "stdint.h"
#include "stm32f4xx.h"
#include "rm_motor.h"
/* Exported types ------------------------------------------------------------*/

/**
 * @brief ��������������
 * 
 */
// #define UP 0
#define DOWN 1

/** 
  * @brief  Encoder PulseMode type structure definition  
  * @note     
  */
typedef struct
{
	int32_t Cnt;

	float Vel;

} PulseModeType;
/** 
  * @brief  Encoder TimeMode type structure definition  
  * @note     
  */
typedef struct
{
	int32_t Tim; //?????????100us?????

	float TimUite;

	int32_t TimNum; //???????? ??��??us

	float Vel;

} TimeModeType;

/** 
  * @brief  Encoder type structure definition  
  * @note     
  */
typedef struct
{
	TimeModeType TimeMode;

	PulseModeType PulseMode;

	int32_t period;

	int32_t Direct;

} EncoderType;

/** 
  * @brief  Commutation type structure definition  
  * @note     
  */
typedef struct
{
	int32_t Mode;

} CommutationType;

/** 
  * @brief  Commutation type structure definition  
  * @note     
  */
typedef struct
{
	int32_t can_status;

	int32_t canId;

} CommandType;

/** 
  * @brief  VelCtrl type structure definition  
  * @note     
  */
typedef struct
{
	float speed;

	float desiredVel[3];

	float velErr;

	float acc;

	float dec;

	float kp;

	float ki;

	float iOut;

	float output;

	float maxOutput;

} VelCtrlType;

/** 
  * @brief  PosCtrl type structure definition  
  * @note     
  */
typedef struct
{
	float actualPos;

	float desiredPos;

	float posErr, posErrLast;

	float posVel, acc;

	float basicPos;

	float kp;

	float kd;

	float output;

} PosCtrlType;

/** 
  * @brief  CurCtrl type structure definition  
  * @note     
  */
typedef struct
{
	float maxCur;

	float desireCur;

	float output;

} CurCtrlType;

/** 
  * @brief  HomingMode type structure definition  
  * @note     
  */
typedef struct
{
	float vel;

	float current;

	float initPos;

	int32_t cnt;

	float output;

} HomingModeType;

/**
 * @brief ShootMode type structure definiton
 * 
 */
typedef struct 
{
	float vel;

	float current;

	float rotatePos;

	int32_t cnt;

	float output;

} ShootModeType;


/** 
  * @brief  Driver type structure definition  
  * @note     
  */
typedef struct
{
	uint32_t unitMode;

	int32_t time;

	FunctionalState status;

	float output;

	VelCtrlType velCtrl;

	PosCtrlType posCtrl;

	CurCtrlType curCtrl;

	HomingModeType homingMode;

	ShootModeType shootMode;

	EncoderType encoder;

	CommutationType commutation;

	CommandType command;

} DriverType;

/** 
  * @brief  Control type structure definition  
  * @note     
  */
typedef struct
{
	float DesiredValue[8];

	float KP[4];

	float KI[4];

} CtrlType;

/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/********************FOC控制参数******************************/
#define VOL_AMP 1.10f		   //Voltage amplitude ?????????????????0.8--13.6A
#define VOL_MAX 18.00f		   //???????
#define VOL_BLIND_AREA 0.80f   //???????????????????????????
#define EMF_CONSTANT 0.020926f //?�T?????????????????? = ??????(V)/???(pulse/ms)

#define CURRENT_MAX_3508 20.0f
#define CURRENT_MAX_2006 8.0f
#define VOLTAGE_MAX_6020 30.0f //ֵ-30000~30000

#define VEL_MAX_3508 10000.0f //??????
#define VEL_KP_3508 0.1f	 //????Kp
#define VEL_KI_3508 0.001f	 //????Ki
#define POS_KP_3508 0.11f
#define POS_KD_3508 0.0f

#define VEL_MAX_2006 10000.0f //??????
#define VEL_KP_2006 0.05f	 //????Kp
#define VEL_KI_2006 0.003f	 //????Ki
#define POS_KP_2006 0.027f
#define POS_KD_2006 0.05f

#define VEL_MAX_6020 10000.0f
#define VEL_KP_6020 0.08f
#define VEL_KI_6020 0.004f
#define POS_KP_6020 0.03f
#define POS_KD_6020 0.07f

//控制模式
#define SPEED_CONTROL_MODE 2
#define POSITION_CONTROL_MODE 5
#define HOMING_MODE 6
#define CUR_CONTROL_MODE 7
#define SHOOT_MODE 8
//TODO ROBONCON2019遗留代码，待删除
#define CMD 0
#define SOFT 1
#define MAX_V 2

//控制代码
#define BLDC_MODE 1
#define FOC_MODE 2

#define CAN_ID_NUM 5

/* Exported functions ------------------------------------------------------- */
float OutPutLim(float val);
float VelSlope(VelCtrlType *velPid);
float VelPidCtrl(VelCtrlType *velPid);
float PosCtrl(PosCtrlType *posPid);
//float 	VelCtrl(float cmdVel);
void VelCtrlInit(void);
void PosCtrlInit(void);
//float	 	CalculSpeed(void);
float CalculSpeed_Pos(DriverType *driver, MotorType *motor);
float GetVelPidOut(void);
float GetSpeed(void);
float GetPosPidOut(void);
float MaxMinLimit(float val, float limit);
void DriverInit(void);
void MotorCtrl(void);
void HomingMode(DriverType *driver);
void ShootMode(DriverType *driver);
void HomingModeInit(void);
void MotorOn(int n);
void MotorOff(int n);
void VelCtrlTest(float vel, int tim);
void RM_3508_init(DriverType *driver, uint8_t mode);
void M_2006_init(DriverType *driver, uint8_t mode);
void ShootMode_init(ShootModeType *shootmode, float vel, float rotatePos);
extern int safe_mode_flag; 
extern int motorType[8];
extern int waveNum;
extern float STVEL;
extern int32_t maxShootVel;
extern DriverType Driver[8];

#endif

/****************** (C) COPYRIGHT 2016 ACTION *****END OF FILE*************/
